import 'package:compact_dio/compact_dio.dart';

class TestDecoder extends BaseDecoder {
  @override
  R decode<R, T>({required Response response, T? type}) {
    final data = response.data;

    /// 业务逻辑错误抛出异常或者做其他处理
    if (data['msg'] != 'success') {
      throw CompactDioException(-1, data['msg']);
    }
    final result = data['data'];
    if (type is BaseModel) {
      if (result is List) {
        final dataList = result.map((e) => type.fromJson(e)).toList();
        return dataList as R;
      } else {
        return type.fromJson(result) as R;
      }
    } else {
      return result as R;
    }
  }
}
